package com.zengdada.authority.utils.where;


import com.alibaba.fastjson.JSONObject;
import com.zengdada.authority.utils.CommonUtils;
import com.zengdada.authority.utils.PublicSearchRangeAssist;
import com.zengdada.authority.utils.where.result.AnalysisResult;
import com.zengdada.authority.utils.where.result.AnalysisResultMerge;
import com.zengdada.authority.utils.where.result.MapperAnalysisResultMerge;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsa
 * @date 2020/9/10 14:10
 * @description Mapper 对应的解析
 */
public class MapperWhere extends Where {

    List<String> fields;

    public MapperWhere setFields(List<String> fields) {
        this.fields = fields;
        return this;
    }

    public MapperAnalysisResultMerge analysisMerge() {
        return new MapperAnalysisResultMerge(analysis());
    }

    public AnalysisResult analysis() {

        if (!isValid()) {
            return null;
        }

        if (fields == null || fields.isEmpty()) {
            throw new RuntimeException("MapperWhere 必须设置字段信息");
        }

        AnalysisResult result = new AnalysisResult();
        if (isRel()) {
            result.setRel(getFormula().toUpperCase());
            for (Where param : getParams()) {
                if (param == null) {
                    continue;
                }
                MapperWhere dmlWhere = JSONObject.parseObject(JSONObject.toJSONString(param), MapperWhere.class);
                dmlWhere.setFields(fields);
                AnalysisResultMerge analysisResultMerge = new AnalysisResultMerge(dmlWhere.analysis());
                result.addAnalysisResultMerge(analysisResultMerge);
            }
        } else {
            if (!fields.contains(CommonUtils.humpToLine(getField()).toLowerCase()) && !fields.contains(CommonUtils.humpToLine(getField()))) {
                throw new RuntimeException(String.format("语句中不存在的查询字段 ‘%s’ ！！", getField()));
            }
            String field = getField();
            if (fields.contains(CommonUtils.humpToLine(getField()))) {
                field = CommonUtils.humpToLine(getField());
            }
            if (fields.contains(CommonUtils.humpToLine(getField()).toLowerCase())) {
                field = CommonUtils.humpToLine(getField()).toLowerCase();
            }
            Object val = getConditionVal();
            List<String> extraFilterContents = new ArrayList<>();
            if (val != null) {
                if (val instanceof List) {
                    ((List<Object>) val).forEach(i -> {
                        result.addParamValue(i);
                        extraFilterContents.add("{?}");
                    });
                } else if (val instanceof PublicSearchRangeAssist) {
                    result.addParamValue(((PublicSearchRangeAssist) val).getFront());
                    result.addParamValue(((PublicSearchRangeAssist) val).getAfter());
                    result.addExtraFilter(String.format("%s {?} AND {?} %s", getConditionAndPrefix(field), getConditionSuffix()));
                    return result;
                } else {
                    result.addParamValue(val);
                    extraFilterContents.add("{?}");
                }
            }
            String sql = String.format("%s %s %s", getConditionAndPrefix(field), String.join(", ", extraFilterContents), getConditionSuffix());
            result.addExtraFilter(sql);
        }
        return result;
    }
}
